/*
 * adc.c
 *
 *  Created on: 2015-03-08
 *      Author: atreyu
 */

#include "adc.h"

void initADC0(void)
{
	sequence = 0;

	bt_UARTsend("initing adc\r\n");

	ROM_SysCtlADCSpeedSet(SYSCTL_ADCSPEED_250KSPS);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
	ROM_ADCHardwareOversampleConfigure(ADC0_BASE, 64);
	ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, _ADC);

	//bt_UARTsend("1\r\n");

	ROM_ADCSequenceDisable(ADC0_BASE, sequence);

	//bt_UARTsend("2\r\n");

	ROM_ADCSequenceConfigure(ADC0_BASE, sequence, ADC_TRIGGER_TIMER, 0);

	ROM_ADCSequenceStepConfigure(ADC0_BASE, sequence, 0, ADC_CTL_CH3 | ADC_CTL_IE |
                             ADC_CTL_END);

	//bt_UARTsend("3\r\n");

	ROM_ADCIntEnable(ADC0_BASE, sequence);
	ROM_IntEnable(INT_ADC0SS0);

	//bt_UARTsend("4\r\n");

	ROM_ADCSequenceEnable(ADC0_BASE, sequence);

	//bt_UARTsend("5\r\n");

	ROM_ADCIntClear(ADC0_BASE, sequence);

	bt_UARTsend("cleared interrupt for adc\r\n");
}

void adc0inthandler(void)
{
	int temp0,temp1,temp2,temp3;
	// Read ADC Value.
	ROM_ADCSequenceDataGet(ADC0_BASE, sequence, ulADC0_Value);

	//ulTemp_ValueC = (1475 - ((2475 * ulADC0_Value[0])) / 4096) /10;
	//ulTemp_ValueF = ((ulTemp_ValueC * 9) + 160) / 5;

	//sprintf(tempoutput, "Board Temp: %3lu*C\r", ulTemp_ValueC);

	//bt_UARTsend(tempoutput);

	adc_digi = 3.3*(ulADC0_Value[0])/4095;
	//sprintf(voltout, "PE0: %3lu steps %lf V\r", *ulADC0_Value, adc_digi);
	//bt_UARTputc(adc_digi);
	temp0=adc_digi/1000;
	temp1=(adc_digi-temp0*1000)/100;
	bt_UARTsend("adc ");
	bt_UARTputc("48+temp0");
	bt_UARTputc(".");
	bt_UARTputc("48+temp1");
	bt_UARTsend("\r\n");

	//bt_UARTsend(voltout);

	ROM_ADCIntClear(ADC0_BASE, sequence);
}
